﻿using DocumentFormat.OpenXml.Drawing;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataService.Office.Word
{
    public static class XmlPackageColor
    {
        /// <summary>
        /// 颜色名称转成16进制 色值
        /// </summary>
        /// <param name="colorName">颜色名称 如 Red</param>
        /// <returns></returns>
        public static string ColorNametoHx16(string colorName)
        {
            Color rgbColor = ColorNameToRGB(colorName);
            return RGBtoHx16(rgbColor);
        }

        /// <summary>
        /// 颜色名称转RGB
        /// </summary>
        /// <param name="colorName"></param>
        /// <returns></returns>
        public static System.Drawing.Color ColorNameToRGB(string colorName)
        {
            if (string.IsNullOrEmpty(colorName) || !System.Drawing.Color.FromName(colorName).IsKnownColor)
            {
                return System.Drawing.Color.FromArgb(0, 0, 0);//设为黑色
            }
            Color color = Color.FromName(colorName);
            return System.Drawing.Color.FromArgb(color.R, color.G, color.B);
        }
        /// <summary>
        /// 颜色转成RGB
        /// </summary>
        /// <param name="color">如 System.Drawing.Color.Red</param>
        /// <returns></returns>
        public static System.Drawing.Color ColorToRGB(System.Drawing.Color color)
        {
            if (color.IsEmpty)
            {
                return System.Drawing.Color.FromArgb(0, 0, 0);//设为黑色
            }
            return System.Drawing.Color.FromArgb(color.R, color.G, color.B);
        }
        /// <summary>
        /// rgb转成16进制
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static string RGBtoHx16(System.Drawing.Color color)
        {
            if (color.IsEmpty)
                return "000000";
            // string hx16 = System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(color.R, color.G, color.B));
            // hx16 = hx16.Replace("#", "");
           string hx16= $"{color.R:X2}{color.G:X2}{color.B:X2}";
            return hx16;
        }

    }
}
